package com.aaa.outbound.controller;



import com.aaa.entity.outbound.Outorder;
import com.aaa.entity.outbound.Picktask;
import com.aaa.entity.outbound.Review;
import com.aaa.entity.outbound.ReviewDetail;
import com.aaa.entity.system.User;
import com.aaa.outbound.feign.SystemFeign;
import com.aaa.outbound.service.OutorderService;
import com.aaa.outbound.service.ReviewDetailService;
import com.aaa.outbound.service.ReviewService;
import com.aaa.util.JwtUtils;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wjf
 * @since 2020-11-27
 */
@RestController
@RequestMapping("/outbound/review")
public class ReviewController {
    @Autowired
    private ReviewService reviewService;
    @Autowired
    private OutorderService outorderService;
    @Autowired
    private ReviewDetailService reviewDetailService;
    @Autowired
    private SystemFeign systemFeign;

    @PostMapping("searchReview/{page}/{limit}")
    @ApiOperation(value = "根据条件分页查询")
    public R searchReview(@ApiParam(value = "当前页数") @PathVariable Integer page,
                            @ApiParam(value = "每页个数") @PathVariable Integer limit,
                            @ApiParam(value = "查询条件") @RequestBody Review review){
        Page<Review> reviewPage=reviewService.searchReview(page,limit,review);
        return R.ok().message("分页查询成功").code(200).data("review",reviewPage);
    }

    @PostMapping("addReview/{outorderno}")
    @ApiOperation(value = "出库复核")
    public R addReview(@ApiParam(value = "出库订单编号")@PathVariable("outorderno") String outorderno, HttpServletRequest request){
        String token = request.getHeader("token");
        Long userid = JwtUtils.getMemberIdByJwtToken(token);
        User user = systemFeign.selectUserById(userid);
        QueryWrapper<Review> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("outorderno",outorderno);
        Review one = reviewService.getOne(queryWrapper);
        if(one == null) {
            List<Outorder> outorderList = outorderService.list();
            for (int i = 0; i < outorderList.size(); i++) {
                if (outorderList.get(i).getOutorderno().equals(outorderno) && outorderList.get(i).getState().equals("4")) {
                    Review review = new Review();
                    Long id = outorderList.get(i).getId();
                    review.setOutorderid(id);
                    review.setOutorderno(outorderno);
                    review.setState("2");
                    review.setCreator(user.getName());
                    Date now = new Date();
                    review.setCreatetime(now);
                    reviewService.save(review);
                    return R.ok().message("复核添加成功").code(200);
                }
            }
            return R.error().message("该订单不存在或该订单未拣货");
        }else {
            return R.error().message("该订单已复核");
        }
    }

    @PostMapping("deleteReview/{outorderno}")
    @ApiOperation("删除复核订单")
    public R deleteReview(@ApiParam(value = "出库订单编号")@PathVariable("outorderno") String outorderno){
        QueryWrapper<Review> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("outorderno",outorderno);
        Review review = reviewService.getOne(queryWrapper);
        Long pid = review.getId();
        List<ReviewDetail> reviewDetails = reviewService.selectReviewDetailByPid(pid);
        if(reviewDetails.size()!=0){
            for(ReviewDetail reviewDetail:reviewDetails){
                reviewDetailService.removeById(reviewDetail);
                reviewService.removeById(review);
            }
            return R.ok().message("删除成功").code(200);
        }else {
            reviewService.removeById(review);
            return R.ok().message("删除成功").code(200);
        }
    }

    @PostMapping("searchOutorderno")
    @ApiOperation(value = "查询所有出库订单编号")
    public R searchOutorderno(){
        List<Outorder> list = outorderService.list();
        List list1=new ArrayList();
        for(int i=0;i<list.size();i++){
            String outorderno = list.get(i).getOutorderno();
            list1.add(outorderno);
        }
        return R.ok().data("outordernos",list1).code(200);
    }
}

